const express = require('express')
const router = express.Router();
const websiteName = ['华军下载站', 'pcsoft下载站', '188软件园', '51下载', 'pc6下载', 
					 '天空下载站', '121下载站', '多多软件站', '下载吧', '西西软件园',
					 'ZOL下载', '多特软件下载', '非凡软件下载', '下载之家', '太平洋下载站',
					 '统一下载站', '站长下载', '极速下载', '七喜下载站', '完美下载站'];	
const key_words = ['qq', '下载站', '软件', '编辑器', '网页', '页游', '网游', '微信', '软件园', '华军', 'pcsoft', 'pc6', 
				   '游戏', '彩票软件', '办公软件', '视频转换', '商业贸易', '网页浏览', '下载工具', '系统辅助', '下载工具', '图像处理',
				   '360卫士', '手机淘宝', '搜狗输入法', '抢票软件', '安卓应用', '小程序', '腾讯视频', '软件资讯', '虾米音乐', '天天飞车',
				   '跳一跳', 'wps', '友情链接', '热门软件'
				   ]					 

	function getTimeArr(oldDate) {
		var date = new Date(), dataArr = [], currentYear = date.getFullYear(), currentMonth = date.getMonth()+1, currentDay = date.getDate();
		if(oldDate.getFullYear() === currentYear) {
			for(let index = oldDate.getMonth()+1; index <= currentMonth; index++) {
	   			switch (index) {
	   				case 1:
	   				case 3:
	   				case 5: 
	   				case 7: 
	   				case 8: 
	   				case 10:
	   				case 12:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 31; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 31; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					
	   					break;
	   				case 2:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 28; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						
	   						for(let m = 1; m <= 28; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;
	   				case 4:
	   				case 6:
	   				case 9:
	   				case 11:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 30; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 30; m++) {
	   							dataArr.push(currentYear + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;		
	   			}
   			}
		}else {
			
			for(let year = oldDate.getFullYear(); year <= currentYear; year++) {
				if(year !== currentYear) {
					for(let index = oldDate.getMonth()+1; index <= 12; index++) {
	   				
	   			switch (index) {
	   				case 1:
	   				case 3:
	   				case 5: 
	   				case 7: 
	   				case 8: 
	   				case 10:
	   				case 12:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 31; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   							
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 31; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;
	   				case 2:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 28; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 28; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;
	   				case 4:
	   				case 6:
	   				case 9:
	   				case 11:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 30; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 30; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;		
	   				}
   				}
				}else {
					for(let index = 1; index <= currentMonth; index++) {
	   			
	   			switch (index) {
	   				case 1:
	   				case 3:
	   				case 5: 
	   				case 7: 
	   				case 8: 
	   				case 10:
	   				case 12:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 31; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 31; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					
	   					// return 31;
	   					break;
	   				case 2:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 28; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 28; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;
	   				case 4:
	   				case 6:
	   				case 9:
	   				case 11:
	   					if(index === oldDate.getMonth()+1) {
	   						for(let m = oldDate.getDate()+1; m <= 30; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else if(index === currentMonth) {
	   						for(let m = 1; m <= currentDay; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}else {
	   						for(let m = 1; m <= 30; m++) {
	   							dataArr.push(year + '/' + index + '/' + m + '/')
	   						}
	   					}
	   					break;		
	   			}
   			}
				}	
			}
		}
		
   		return dataArr;
	}

	function GetDateStr(AddDayCount) { 
		var dd = new Date(), currentYear = dd.getFullYear(), currentMonth = dd.getMonth()+1, currentDay = dd.getDate(); 
	    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期    
	    var y = dd.getFullYear();     
	    var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期，不足10补0    
	    var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号，不足10补0
	    
	    var dataArr = getTimeArr(dd);
	   	return dataArr;     
	}

router.post('/industry', function (req, res) {
	console.log('req.body', req.body);


	let defaultData = {
	}
	

	function setData(arr) {
		let length = arr.length;
		let items = [];
		for(let index = 0; index < length; index++) {
			let obj = {};
			let date = new Date(arr[index]).getTime()
			obj.time = date;
			let down = Math.ceil(Math.random() * 100);           //生成0——1的随机数  
			let other = Math.ceil(Math.random() * 100); 
         	obj.download = down;
         	obj.other = other;
         	items[index] = obj;
		}
		defaultData.items = items;
		data.data = defaultData;
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
			}
	if(req.body.time === '30') {
		let time = GetDateStr(-30)
		if(req.body.page_number === 'all') {
			setData(time);
		}
		if(req.body.page_number === '1') {
			setData(time);
		}
		if(req.body.page_number === '2') {
			setData(time);
		}

	}		
	if(req.body.time === '90') {
		let time = GetDateStr(-90)
		setData(time);
		// data.data = returnData
	}
	if(req.body.time === '180') {
		let time = GetDateStr(-180)
		setData(time);
		// data.data = returnData
	}
    res.send(data) 
})


router.post('/website_key', function (req, res) {
	console.log('req.body', req.body);
	let defaultData = {
		items: []
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
			};
				
	let nameLength = websiteName.length;
	function setWebData(name,timeArr) {
		let length = timeArr.length;
		let obj = {
			name: name,
			data: {}
		};
		for(let index = 0; index < length; index++) {
			
			let date = new Date(timeArr[index]).getTime();

			let value = Math.ceil(Math.random() * 100); 
			obj.data[date] = value;	
		}
		return 	obj;
	}		
	if(req.body.time === '30') {
		let time = GetDateStr(-30);
		let web_data = [];
		for(let index=0; index<nameLength; index++) {
			web_data[index] = setWebData(websiteName[index], time)
		}

		defaultData.items = web_data;
		data.data = defaultData;
	}		
	res.send(data);
})

router.get('/website_latest', function (req, res) {
	let nameLength = websiteName.length;
	let defaultData = {
		items: []
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
			};	
	function sendData() {
		for(let index=0; index<nameLength; index++) {
			let all_count, all_trend, page1_count, page1_trend, page2_count, page2_trend, rise_page1, drop_page2, change;
			page1_count = Math.ceil(Math.random() * 1000);
			page2_count = Math.ceil(Math.random() * 1000);
			page1_trend = (page1_count % 2 == 0) ? '+' + Math.ceil(Math.random() * 100): '-' + Math.ceil(Math.random() * 100);
			page2_trend = (page2_count % 2 == 0) ? '+' + Math.ceil(Math.random() * 100): '-' + Math.ceil(Math.random() * 100);
			all_count = page1_count + page2_count;
			all_trend = (all_count % 2 == 0) ? '+' + Math.ceil(Math.random() * 100): '-' + Math.ceil(Math.random() * 100)
			rise_page1 = Math.ceil(Math.random() * 100);
			drop_page2 = Math.ceil(Math.random() * 100);
			let change1 =  rise_page1 - drop_page2;
			if(change1 > 0) {
				change = '+' + change1;
			}else {
				change = change1.toString();
			}
			
			let obj = {
				id: index,
				name: websiteName[index],
				all_count: all_count,
				all_trend: all_trend,
				page1_count: page1_count,
				page1_trend: page1_trend,
				page2_count: page2_count,
				page2_trend: page2_trend,
				rise_page1: rise_page1,
				drop_page2: drop_page2,
				change: change
			}
			defaultData.items.push(obj);
		}
	}
	sendData();
	res.send(data);		
})
router.post('/website_detail', function(req, res) {  //某个站点的涨跌明细
	let defaultData = {
		items: [],
		name: ''
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};
	function detail(type) {
		for(let index = 0; index < 15; index++) {
			let key_id = Math.ceil(Math.random() * 35);

			let obj = {
				key_name: key_words[key_id],
				pc_expoment: Math.ceil(Math.random() * 100),
				mobile_expoment: Math.ceil(Math.random() * 100),
				name360_expoment: Math.ceil(Math.random() * 100),
				all_expoment: Math.ceil(Math.random() * 100),
				old_rank: Math.ceil(Math.random() * 20),
				new_rank: Math.ceil(Math.random() * 10)
			}
			if(type === 'rise') {
				// let timer = setInterval(function() {
					if(obj.old_rank > obj.new_rank) {
						// clearInterval(timer)
					}else {

						obj.new_rank = obj.old_rank - 1
					}
				// }, 0)
				
			}
			if(type === 'drop') {
				if(obj.old_rank < obj.new_rank) {
						// clearInterval(timer)
					}else {

						obj.new_rank = obj.old_rank + Math.ceil(Math.random() * 5)
					}
			}	
			defaultData.items[index] = obj;
		}
	}
	if(req.body.website_name == null) { //无站点ID
		res.send({
			status: 0,
			message: '请选择要查询的站点'
		})
	}else {
		let name = req.body.website_name;
		console.log(req.body)
		defaultData.name = name;
		if(req.body.type === 'rise') {
			
			detail('rise');
		}
		if(req.body.type === 'drop') {
			detail('drop');
		}
		res.send(data);
	}
})

router.post('/key_words', function(req, res){ //
	let defaultData = {
		items: [],
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};

	if(req.body.keywords_name == null) {
		res.send({
			status: 0,
			message: '请选择要查询的关键字'
		})
	}else {

		if(req.body.time === '30') {
			let time = GetDateStr(-30)
			mydata(time);
		}
		if(req.body.time === '90') {
			let time = GetDateStr(-90)
			mydata(time);
		}
		res.send(data);
	}
	function mydata(time) {
		let length = time.length;
		let mylength = websiteName.length;

		for(let index = 0; index < mylength; index++) {
			let obj = {
				name: websiteName[index],
				data: {}
			};
			for(let j = 0 ; j < length; j++) {
				
				let date = new Date(time[j]).getTime();
				obj.data[date] = Math.ceil(Math.random() * 20);
			}
			defaultData.items[index] = obj;
		}
	}
})


router.post('/website_list', function(req, res) { // 某个站点的关键词的数量明细
	let defaultData = {
		items: [],
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};
	function setnewdata(time, name) {
		defaultData.items = [];
		let length = time.length;
		let mylength = websiteName.length;

		// for(let index = 0; index < mylength; index++) {
			let obj = {
				name: name,
				data: {}
			};
			for(let j = 0; j < length; j++) {
				let date = new Date(time[j]).getTime();
				obj.data[date] = Math.ceil(Math.random() * 100);
			}
			defaultData.items.push(obj);
		// }
	}
	if(req.body.website_name == null) {
		res.send({
			status: 0,
			message: '参数不合法'
		})
	}else {
		if(req.body.time === '30'){
			let time = GetDateStr(-30);
			let name = req.body.website_name;
			setnewdata(time, name);

		}
		res.send(data)
	}
})

router.post('/comparison', function(req, res) { //
	let defaultData = {
		items: [],
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};
	function mynew(arr) {
		let length = arr.length;
		let namelength = websiteName.length;

		for(let index = 0; index < namelength; index++) {
			let obj = {
				name: websiteName[index],
				id: index,
				data: {}
			}
			for(let j = 0; j< length; j++) {
				let date = new Date(arr[j]).getTime();
				obj.data[date] = Math.ceil(Math.random() * 100)
			}
			
			defaultData.items[index] = obj;
		}
	}
	if(req.body.type == null) {

	}else {
		if(req.body.time === '30') {
			let time = GetDateStr(-30);
			mynew(time);
			res.send(data);
		}
		
	}
})


router.post('/comparison_detail', function(req, res) {
	let defaultData = {
		items: [],
		name: '',
		time: null,
		id: null
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};
	let time;
	if(req.body.web_id === null || req.body.type === null) {
		res.send({
			status: 0,
			message: '参数不合法'
		})
	}else {
		if(req.body.time === null) {
			time = new Date()
		}else {
			time = req.body.time;
		}
		detailData(req.body.id, req.body.type)
		res.send(data)
	}
	function detailData(id, type) {
		let website_id = id, websiteNameLength = websiteName.length;
		defaultData.name = websiteName[id];
		defaultData.id = id;
		defaultData.time = time;
		for(let index = 0; index < Math.ceil(Math.random() * 20); index++) {
			let key_id = Math.ceil(Math.random() * 35);
			let obj = {
				key_name: key_words[key_id],
				pc_expoment: Math.ceil(Math.random() * 100),
				mobile_expoment: Math.ceil(Math.random() * 100),
				name360_expoment: Math.ceil(Math.random() * 100),
				all_expoment: Math.ceil(Math.random() * 100),
				old_rank: Math.ceil(Math.random() * 20),
				new_rank: Math.ceil(Math.random() * 10)
			}
			if(type === 'rise') {
				// let timer = setInterval(function() {
					if(obj.old_rank > obj.new_rank) {
						// clearInterval(timer)
					}else {

						obj.new_rank = obj.old_rank - 1
					}
				// }, 0)
				
			}
			if(type === 'drop') {
				if(obj.old_rank < obj.new_rank) {
						// clearInterval(timer)
					}else {

						obj.new_rank = obj.old_rank + Math.ceil(Math.random() * 5)
					}
			}
			defaultData.items[index] = obj;
		}
	}

})
router.post('/directory', function(req, res){
	let defaultData = {
		items: [],
		directory: [
			{name: '/', count: 999},
			{name: '/soft', count: 99},
			{name: '/zt', count: 34},
			{name: '/sort', count: 90},
			{name: '/news', count: 67},
			{name: '/games', count: 78}
		],
		name: websiteName[req.body.website_name]
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};
	function set(direct_name) {
		if(direct_name != '/') {
			defaultData.directory = []
		}
		for(let index = 0; index < 10; index++) {
			let obj = {
				key_name: key_words[Math.ceil(Math.random() * 35)],
				pc_expoment: Math.ceil(Math.random() * 100),
				mobile_expoment: Math.ceil(Math.random() * 100),
				name360_expoment: Math.ceil(Math.random() * 100),
				all_expoment: Math.ceil(Math.random() * 100),
				rank: Math.ceil(Math.random() * 10)
			}
			defaultData.items[index] = obj;
		}
	}
	if(req.body.website_name == null || req.body.direct_name == null ) {
		res.send({
			status: 0,
			message: '请选择要查询的站点'
		})
	}else {

		set(req.body.direct_name);
		res.send(data);
	}
})

router.post('/industry_list', function(req, res){ //列表
	let defaultData = {
		items: [],
	}
	let data = {
		status: 1,
		message: '获取数据成功',
		data: defaultData
	};
	for(let index = 0; index < websiteName.length; index++) {
		let obj = {
			id: index,
			name: websiteName[index],
			domain: 'www.baidu.com'
		};
		defaultData.items[index] = obj;

	}
	res.send(data)
})
router.post('/delete_website', function(req, res) { //删除
	console.log(req.body);
	if(req.body.id == null) {

	}else {
		console.log(req.body)
		res.send({
			status:1,
			message: '删除成功'
		})
	}
})

router.post('/add_website', function(req, res) { //添加
	res.send({
		status: 1,
		message: '添加成功'
	})
})

// router.post('/industry', function (req, res) {
module.exports = router
